package com.haliyoo.adhere.http;

import java.io.IOException;
import java.net.SocketException;
import java.net.SocketTimeoutException;

import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;

import com.haliyoo.adhere.bean.PacketBean;

import android.util.Log;

/**
 * 发送HTTP请求接口类(返回HttpData)
 * @author wation.haliyoo
 *
 */

public class HttpClient {

	private static final String TAG = "HttpClient";

	/**
	 * 登录
	 * 
	 * @param serverAddr
	 *            服务器地址
	 * @param userId
	 *            用户ID
	 * @param password
	 *            密码
	 * @return 接收的数据
	 * @throws SocketTimeoutException
	 * @throws SocketException
	 * @throws ClientProtocolException
	 * @throws IOException
	 */
	public static HttpData loginRequest(String serverAddr, String userId,
			String password) throws SocketTimeoutException, SocketException,
			ClientProtocolException, IOException {

		String url = PacketBean.assembleLoginRequest(serverAddr, userId,
				password);
		HttpGet get = new HttpGet(url);
		HttpData data = null;
		try {
			data = HttpConnection.getRequest(get);
			if (data == null || data.getByteArray() == null) {
				Log.e(TAG, "None Recv Data.");
			} else {
				Log.i(TAG, "Recv Data:"
						+ (new String(data.getByteArray(), "UTF-8")));
			}
		} catch (SocketTimeoutException e) {
			throw new SocketTimeoutException();
		} catch (SocketException e) {
			throw new SocketException();
		} catch (ClientProtocolException e) {
			throw new ClientProtocolException();
		} catch (IOException e) {
			throw new IOException();
		} finally {
			get.abort();
		}

		return data;
	}

	/**
	 * 退出
	 * 
	 * @param serverAddr
	 *            服务器地址
	 * @param userId
	 *            用户ID
	 * @param password
	 *            密码
	 * @return 接收的数据
	 * @throws SocketTimeoutException
	 * @throws SocketException
	 * @throws ClientProtocolException
	 * @throws IOException
	 */
	public static HttpData logoutRequest(String serverAddr, String userId,
			String password) throws SocketTimeoutException, SocketException,
			ClientProtocolException, IOException {

		String url = PacketBean.assembleLogoutRequest(serverAddr, userId,
				password);
		HttpGet get = new HttpGet(url);
		HttpData data = null;
		try {
			data = HttpConnection.getRequest(get);
			if (data == null || data.getByteArray() == null) {
				Log.e(TAG, "None Recv Data.");
			} else {
				Log.i(TAG, "Recv Data:"
						+ (new String(data.getByteArray(), "UTF-8")));
			}
		} catch (SocketTimeoutException e) {
			throw new SocketTimeoutException();
		} catch (SocketException e) {
			throw new SocketException();
		} catch (ClientProtocolException e) {
			throw new ClientProtocolException();
		} catch (IOException e) {
			throw new IOException();
		} finally {
			get.abort();
		}

		return data;
	}

	/**
	 * 注册
	 * 
	 * @param serverAddr
	 *            服务器地址
	 * @param userId
	 *            用户ID
	 * @param password
	 *            密码
	 * @param userName
	 *            用户名
	 * @return 接收的数据
	 * @throws SocketTimeoutException
	 * @throws SocketException
	 * @throws ClientProtocolException
	 * @throws IOException
	 */
	public static HttpData registerRequest(String serverAddr, String userId,
			String password, String userName) throws SocketTimeoutException,
			SocketException, ClientProtocolException, IOException {

		String url = PacketBean.assembleRegisterRequest(serverAddr, userId,
				password, userName);
		HttpGet get = new HttpGet(url);
		HttpData data = null;
		try {
			data = HttpConnection.getRequest(get);
			if (data == null || data.getByteArray() == null) {
				Log.e(TAG, "None Recv Data.");
			} else {
				Log.i(TAG, "Recv Data:"
						+ (new String(data.getByteArray(), "UTF-8")));
			}
		} catch (SocketTimeoutException e) {
			throw new SocketTimeoutException();
		} catch (SocketException e) {
			throw new SocketException();
		} catch (ClientProtocolException e) {
			throw new ClientProtocolException();
		} catch (IOException e) {
			throw new IOException();
		} finally {
			get.abort();
		}

		return data;
	}

	/**
	 * 更新用户信息
	 * 
	 * @param serverAddr
	 *            服务器地址
	 * @param userId
	 *            用户ID
	 * @param userName
	 *            用户名
	 * @param sign
	 *            签名
	 * @return 接收的数据
	 * @throws SocketTimeoutException
	 * @throws SocketException
	 * @throws ClientProtocolException
	 * @throws IOException
	 */
	public static HttpData updateInfoRequest(String serverAddr, String userId,
			String userName, String sign) throws SocketTimeoutException,
			SocketException, ClientProtocolException, IOException {

		String url = PacketBean.assembleUpdateInfoRequest(serverAddr, userId,
				userName, sign);
		HttpGet get = new HttpGet(url);
		HttpData data = null;
		try {
			data = HttpConnection.getRequest(get);
			if (data == null || data.getByteArray() == null) {
				Log.e(TAG, "None Recv Data.");
			} else {
				Log.i(TAG, "Recv Data:"
						+ (new String(data.getByteArray(), "UTF-8")));
			}
		} catch (SocketTimeoutException e) {
			throw new SocketTimeoutException();
		} catch (SocketException e) {
			throw new SocketException();
		} catch (ClientProtocolException e) {
			throw new ClientProtocolException();
		} catch (IOException e) {
			throw new IOException();
		} finally {
			get.abort();
		}

		return data;
	}

	/**
	 * 修改密码
	 * 
	 * @param serverAddr
	 *            服务器地址
	 * @param userId
	 *            用户ID
	 * @param password
	 *            密码
	 * @param newPassword
	 *            新密码
	 * @param sign
	 *            签名
	 * @return 接收的数据
	 * @throws SocketTimeoutException
	 * @throws SocketException
	 * @throws ClientProtocolException
	 * @throws IOException
	 */
	public static HttpData updatePasswordRequest(String serverAddr,
			String userId, String password, String newPassword, String sign)
			throws SocketTimeoutException, SocketException,
			ClientProtocolException, IOException {

		String url = PacketBean.assembleUpdatePasswordRequest(serverAddr,
				userId, password, newPassword, sign);
		HttpGet get = new HttpGet(url);
		HttpData data = null;
		try {
			data = HttpConnection.getRequest(get);
			if (data == null || data.getByteArray() == null) {
				Log.e(TAG, "None Recv Data.");
			} else {
				Log.i(TAG, "Recv Data:"
						+ (new String(data.getByteArray(), "UTF-8")));
			}
		} catch (SocketTimeoutException e) {
			throw new SocketTimeoutException();
		} catch (SocketException e) {
			throw new SocketException();
		} catch (ClientProtocolException e) {
			throw new ClientProtocolException();
		} catch (IOException e) {
			throw new IOException();
		} finally {
			get.abort();
		}

		return data;
	}

	/**
	 * 发布
	 * 
	 * @param serverAddr
	 *            服务器地址
	 * @param userId
	 *            用户ID
	 * @param adId
	 *            广告ID
	 * @param sign
	 *            签名
	 * @return 接收的数据
	 * @throws SocketTimeoutException
	 * @throws SocketException
	 * @throws ClientProtocolException
	 * @throws IOException
	 */
	public static HttpData postRequest(String serverAddr, String userId,
			String adId, String sign) throws SocketTimeoutException,
			SocketException, ClientProtocolException, IOException {

		String url = PacketBean.assemblePostRequest(serverAddr, userId, adId,
				sign);
		HttpGet get = new HttpGet(url);
		HttpData data = null;
		try {
			data = HttpConnection.getRequest(get);
			if (data == null || data.getByteArray() == null) {
				Log.e(TAG, "None Recv Data.");
			} else {
				Log.i(TAG, "Recv Data:"
						+ (new String(data.getByteArray(), "UTF-8")));
			}
		} catch (SocketTimeoutException e) {
			throw new SocketTimeoutException();
		} catch (SocketException e) {
			throw new SocketException();
		} catch (ClientProtocolException e) {
			throw new ClientProtocolException();
		} catch (IOException e) {
			throw new IOException();
		} finally {
			get.abort();
		}

		return data;
	}

	/**
	 * 删除
	 * 
	 * @param serverAddr
	 *            服务器地址
	 * @param userId
	 *            用户ID
	 * @param adId
	 *            广告ID
	 * @param sign
	 *            签名
	 * @return 接收的数据
	 * @throws SocketTimeoutException
	 * @throws SocketException
	 * @throws ClientProtocolException
	 * @throws IOException
	 */
	public static HttpData deleteRequest(String serverAddr, String userId,
			String adId, String sign) throws SocketTimeoutException,
			SocketException, ClientProtocolException, IOException {

		String url = PacketBean.assembleDeleteRequest(serverAddr, userId, adId,
				sign);
		HttpGet get = new HttpGet(url);
		HttpData data = null;
		try {
			data = HttpConnection.getRequest(get);
			if (data == null || data.getByteArray() == null) {
				Log.e(TAG, "None Recv Data.");
			} else {
				Log.i(TAG, "Recv Data:"
						+ (new String(data.getByteArray(), "UTF-8")));
			}
		} catch (SocketTimeoutException e) {
			throw new SocketTimeoutException();
		} catch (SocketException e) {
			throw new SocketException();
		} catch (ClientProtocolException e) {
			throw new ClientProtocolException();
		} catch (IOException e) {
			throw new IOException();
		} finally {
			get.abort();
		}

		return data;
	}

	/**
	 * 转发
	 * 
	 * @param serverAddr
	 *            服务器地址
	 * @param userId
	 *            用户ID
	 * @param adIds
	 *            广告ID列表
	 * @param sign
	 *            签名
	 * @return 接收的数据
	 * @throws SocketTimeoutException
	 * @throws SocketException
	 * @throws ClientProtocolException
	 * @throws IOException
	 */
	public static HttpData repostRequest(String serverAddr, String userId,
			String[] adIds, String sign) throws SocketTimeoutException,
			SocketException, ClientProtocolException, IOException {

		String url = PacketBean.assembleRepostRequest(serverAddr, userId, sign, adIds);

		HttpGet get = new HttpGet(url);
		HttpData data = null;
		try {
			data = HttpConnection.getRequest(get);
			if (data == null || data.getByteArray() == null) {
				Log.e(TAG, "None Recv Data.");
			} else {
				Log.i(TAG, "Recv Data:"
						+ (new String(data.getByteArray(), "UTF-8")));
			}
		} catch (SocketTimeoutException e) {
			throw new SocketTimeoutException();
		} catch (SocketException e) {
			throw new SocketException();
		} catch (ClientProtocolException e) {
			throw new ClientProtocolException();
		} catch (IOException e) {
			throw new IOException();
		} finally {
			get.abort();
		}

		return data;
	}
	
//	/**
//	 * 转发
//	 * 
//	 * @param serverAddr
//	 *            服务器地址
//	 * @param userId
//	 *            用户ID
//	 * @param adIds
//	 *            广告ID列表
//	 * @param sign
//	 *            签名
//	 * @return 接收的数据
//	 * @throws SocketTimeoutException
//	 * @throws SocketException
//	 * @throws ClientProtocolException
//	 * @throws IOException
//	 */
//	public static HttpData repostRequest(String serverAddr, String userId,
//			String[] adIds, String sign) throws SocketTimeoutException,
//			SocketException, ClientProtocolException, IOException {
//
//		String url = PacketBean.assembleRepostRequest(serverAddr, userId, sign);
//		String xml = PacketBean.assembleRepostContent(adIds);
//		
//		HttpPost post = new HttpPost(url);
//		HttpData data = null;
//		try {
//			data = HttpConnection.postRequest(post, xml);
//			if (data == null || data.getByteArray() == null) {
//				Log.e(TAG, "None Recv Data.");
//			} else {
//				Log.i(TAG, "Recv Data:"
//						+ (new String(data.getByteArray(), "UTF-8")));
//			}
//		} catch (SocketTimeoutException e) {
//			throw new SocketTimeoutException();
//		} catch (SocketException e) {
//			throw new SocketException();
//		} catch (ClientProtocolException e) {
//			throw new ClientProtocolException();
//		} catch (IOException e) {
//			throw new IOException();
//		} finally {
//			post.abort();
//		}
//
//		return data;
//	}
	
	/**
	 * 获取列表
	 * 
	 * @param serverAddr
	 *            服务器地址
	 * @param userId
	 *            用户ID
	 * @param sign
	 *            签名
	 * @return 接收的数据
	 * @throws SocketTimeoutException
	 * @throws SocketException
	 * @throws ClientProtocolException
	 * @throws IOException
	 */
	public static HttpData listRequest(String serverAddr, String userId,
			String sign) throws SocketTimeoutException, SocketException,
			ClientProtocolException, IOException {

		String url = PacketBean.assembleListRequest(serverAddr, userId, sign);
		HttpGet get = new HttpGet(url);
		HttpData data = null;
		try {
			data = HttpConnection.getRequest(get);
			if (data == null || data.getByteArray() == null) {
				Log.e(TAG, "None Recv Data.");
			} else {
				Log.i(TAG, "Recv Data:"
						+ (new String(data.getByteArray(), "UTF-8")));
			}
		} catch (SocketTimeoutException e) {
			throw new SocketTimeoutException();
		} catch (SocketException e) {
			throw new SocketException();
		} catch (ClientProtocolException e) {
			throw new ClientProtocolException();
		} catch (IOException e) {
			throw new IOException();
		} finally {
			get.abort();
		}

		return data;
	}

	/**
	 * 获取创意/魅力/贡献/童鞋的数目
	 * 
	 * @param serverAddr
	 *            服务器地址
	 * @param userId
	 *            用户ID
	 * @param sign
	 *            签名
	 * @param customerId
	 *            待查用户id
	 * @return 接收的数据
	 * @throws SocketTimeoutException
	 * @throws SocketException
	 * @throws ClientProtocolException
	 * @throws IOException
	 */
	public static HttpData countRequest(String serverAddr, String userId,
			String sign, String customerId) throws SocketTimeoutException, SocketException,
			ClientProtocolException, IOException {

		String url = PacketBean.assembleCountRequest(serverAddr, userId,
				sign, customerId);
		HttpGet get = new HttpGet(url);
		HttpData data = null;
		try {
			data = HttpConnection.getRequest(get);
			if (data == null || data.getByteArray() == null) {
				Log.e(TAG, "None Recv Data.");
			} else {
				Log.i(TAG, "Recv Data:"
						+ (new String(data.getByteArray(), "UTF-8")));
			}
		} catch (SocketTimeoutException e) {
			throw new SocketTimeoutException();
		} catch (SocketException e) {
			throw new SocketException();
		} catch (ClientProtocolException e) {
			throw new ClientProtocolException();
		} catch (IOException e) {
			throw new IOException();
		} finally {
			get.abort();
		}

		return data;
	}
}
